#ifndef MATHUTILS_H
#define MATHUTILS_H

#include "D3DConfiguration.h"

#include <cfloat>

namespace D3D10Utils
{
    // Returns random float in [0, 1).
    __forceinline float RandF()
    {
        return static_cast<float> ( (rand()) / static_cast<float>(RAND_MAX) );
    }

    // Returns random float in [a, b).
    __forceinline float RandF(const float a, const float b)
    {
        return a + RandF() * (b - a);
    }

    // Returns random vector on the unit sphere.
    __forceinline D3DXVECTOR3 RandUnitVec3()
    {
        D3DXVECTOR3 v(RandF(), RandF(), RandF());
        D3DXVec3Normalize(&v, &v);

        return v;
    }

    template<typename T>
    __forceinline T Min(const T& a, const T& b)
    {
        return a < b ? a : b;
    }

    template<typename T>
    __forceinline T Max(const T& a, const T& b)
    {
        return a > b ? a : b;
    }

    template<typename T>
    __forceinline T Lerp(const T& a, const T& b, float t)
    {
        return a + (b - a) * t;
    }

    template<typename T>
    __forceinline T Clamp(const T& x, const T& low, const T& high)
    {
        return x < low ? low : (x > high ? high : x); 
    }

    // Get height (y-coord) based on x and z coordinates.
    __forceinline float getHeight(const float x, const float z)
    {
        return 0.3f * ( z * sinf(0.1f * x) + x * cosf(0.1f * z) );
    }

    const float INFINITY = FLT_MAX;
    const float PI = 3.14159265358979323f;
    const float MATH_EPS = 0.0001f;
}

#endif // MATHUTILS_H
